<template>
  <Layout></Layout>
  <el-dialog class="dialog-token" v-model="dialogTableVisible" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false">
    <div>该机器暂无使用权，请联系云会计客服开通</div>
    <div class="margin">联系电话：15535186706</div>
    <el-button type="primary" @click="reGetToken">重新登录</el-button>
  </el-dialog>
</template>

<script setup>
import { ref, onBeforeMount, onMounted, provide } from 'vue'
import Layout from '@/layout/index.vue'
import { getToken } from '@/apis/index'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { setItem } from '@/utils/stroge'


const store = useStore()
const router = useRouter()
// 控制弹窗的显示隐藏
const dialogTableVisible = ref(false)

// 获取项目的token
const token = () => {
  // const sn = window.android.getRobotSn()
  const sn = 'DF50'
  return getToken(sn).then(res => {
    if(res.data.token){
      dialogTableVisible.value = false
      setItem('token', res.data.token)
    }else{
      dialogTableVisible.value = true
    }
  }).catch(() => {
    dialogTableVisible.value = true
  })
}

onBeforeMount(() => {
  token().then(() => {
    store.dispatch('getPolicyType')
    store.dispatch('getHeaderTitle')
  })
})

onMounted(() => {
  fn()
})

// 重新获取token
const reGetToken = () => {
  token()
}

// 屏幕静止的时间
const pageTime = 10 * 60 * 3000

// 将屏幕回复到默认状态的方法
const resetScreen = () => {
  return router.push({
    name:'chat'
  })
}

let timer = null
// 开启屏幕定时器
const fn = () => {
  if(timer){
    window.clearTimeout(timer)
  }
  timer = window.setTimeout(() => {
    resetScreen().then(() => {
      timer = null
      store.commit('setIsScreen')
    })
  }, pageTime)
}
provide('screenFn', fn)
</script>